// ==========================
// 用户数据配置文件（兜底用）
// ==========================
const userConfig = {
    normalUsers: [
        {
            username: "涂明珏",// 张莫某
            password: "haoyunlai",
            name: "涂明珏",
            position: "专利审查员",
            department: "医学工程室",
            pregnancyStatus: "孕中期 (24周)",
            dueDate: "2025年3月15日",
            maternityLeave: "2025年1月20日"
        },
        {
            username: "王鹤迪",// 李莫某
            password: "haoyunlai",
            name: "王鹤迪",
            position: "专利审查员",
            department: "信息工程室",
            pregnancyStatus: "孕早期 (8周)",
            dueDate: "2025年6月20日",
            maternityLeave: "2025年4月25日"
        },
        {
            username: "黄思怡",// 王莫某
            password: "haoyunlai",
            name: "黄思怡",
            position: "专利审查员",
            department: "医学工程室",
            pregnancyStatus: "产后2月",
            dueDate: "2024年12月1日",
            maternityLeave: "2024年10月5日"
        }
    ],
    adminUsers: [
        {
            username: "admin",
            password: "haoyunlai",
            name: "某处",
            position: "室负责人",
            department: "医学工程室"
        },
        {
            username: "admin_2",
            password: "haoyunlai",
            name: "系统管理",
            position: "系统管理",
            department: "医学工程室"
        }
    ]
};

// ==========================
// 统一对外接口：加载用户数据
// 返回 Promise<{normalUsers[], adminUsers[]}>
// ==========================
function loadUsersFromCSV() {
    const User_CSV_URL = 'https://haoyunlai-12345-1342611250.cos.ap-guangzhou.myqcloud.com/user.csv';
    const User_freshURL = `${User_CSV_URL}?t=${Date.now()}`;

    return fetch(User_freshURL)
        .then(response => {
            if (!response.ok) throw new Error('CSV 拉取失败');
            return response.text();
        })
        .then(text => {
            const normalUsers = [];
            const adminUsers  = [];
            let section = '';

            text.split(/\r?\n/).forEach(raw => {
                const line = raw.trim();
                if (line === '【普通用户】') { section = 'normal'; return; }
                if (line === '【管理者用户】') { section = 'admin';  return; }
                if (!line || line.startsWith('用户名') || !line.includes(',')) return;

                const p = line.split(',').map(s => s.trim());
                const user = {
                    username: p[0],
                    password: p[1],
                    name: p[2],
                    position: p[3] || '',
                    department: p[4] || '',
                    pregnancyStatus: p[5] || '',
                    dueDate: p[6] || '',
                    maternityLeave: p[7] || ''
                };

                section === 'normal' ? normalUsers.push(user) : adminUsers.push(user);
            });

            // 如果 CSV 空列表，也走兜底
            if (normalUsers.length === 0 && adminUsers.length === 0) throw new Error('CSV 无数据');
            return { normalUsers, adminUsers };
        })
        .catch(err => {
            console.warn('CSV 加载失败，启用 JS 兜底数据:', err);
            // 返回深拷贝，避免外部修改污染兜底配置
            return {
                normalUsers: JSON.parse(JSON.stringify(userConfig.normalUsers)),
                adminUsers:  JSON.parse(JSON.stringify(userConfig.adminUsers))
            };
        });
}





